[BITS 32]      ; 32 bit code
[global _start] ; make 'start' function global
[global _hlt]
[global initial_pt]
[extern kmain] ; our C kernel main

; Multiboot constants
MULTIBOOT_PAGE_ALIGN	equ 1<<0
MULTIBOOT_MEMORY_INFO	equ 1<<1
MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

; Multiboot header (needed to boot from GRUB)
ALIGN 4
multiboot_header:
	dd MULTIBOOT_HEADER_MAGIC
	dd MULTIBOOT_HEADER_FLAGS
	dd MULTIBOOT_CHECKSUM

_start:
	CLI ; just to be sure
	mov esp, _sys_stack
	sub esp, 0xc0000000 ; xlate

	push dword 0
	popf
	push ebx	; multiboot lols
	push eax

        mov eax, initial_pt
        sub eax, 0xc0000000 ; xlate addr down
        mov cr3, eax 

        mov eax, cr4
        or eax, 0x10 ; PSE
        mov cr4, eax

        mov eax, cr0
	or eax, 0x80000000 ; PG bit
        mov cr0, eax

	lea eax, [flush]
	jmp eax
flush: 
	; fix stack
	add esp, 0xc0000000

        ; har
        mov byte [0xc00b8000], 1

	call kmain

_hlt:
	cli
	hlt
	jmp $ 


ALIGN 4096
initial_pt:
; bootstrap page table - identity map i guess

dd 0x000083 ; 0mb - 4mb -> ident
dd 0x400083 ; 4mb - 8mb -> ident

; ...

times 766 dd 0 ; skip a bunch o pages 
dd 0x00000083 ; 3gb - 3gb+4mb -> 0mb-4mb
dd 0x00400083 
dd 0x00800083 
dd 0x00C00083 
dd 0x01000083 

times 254 dd 0 ; zero! lolol

; -- NO MORE LOL --

SECTION .bss
    resb 8192               ; This reserves 8KBytes of memory here
_sys_stack:

;; XXXXXXXXXX UGLY
global _kmalloc_fuck_you
_kmalloc_fuck_you:
resb 65536 

